package com.easypay.reactive8583.netty.codec;

import com.solab.iso8583.IsoMessage;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.util.CharsetUtil;

import java.nio.ByteBuffer;

/**
 * @author pandans
 * @date 2021/03/04 16:26
 * @Description:
 */
public class Iso8583Encoder extends MessageToByteEncoder<IsoMessage> {
    private int lengthHeaderLength;
    private boolean encodeLengthHeaderAsString;

    public Iso8583Encoder(int lengthHeaderLength, boolean encodeLengthHeaderAsString) {
        this.lengthHeaderLength = lengthHeaderLength;
        this.encodeLengthHeaderAsString = encodeLengthHeaderAsString;
    }

    @Override
    public void encode(ChannelHandlerContext channelHandlerContext, IsoMessage isoMessage, ByteBuf out) {
        if(lengthHeaderLength == 0) {
            byte[] bytes = isoMessage.writeData();
            out.writeBytes(bytes);
        }else if(encodeLengthHeaderAsString){
            byte[] bytes = isoMessage.writeData();
            String lengthHeader = String.format("%0" + lengthHeaderLength + "d", bytes.length);
            out.writeBytes(lengthHeader.getBytes(CharsetUtil.US_ASCII));
            out.writeBytes(bytes);
        }else {
            ByteBuffer byteBuffer = isoMessage.writeToBuffer(lengthHeaderLength);
            out.writeBytes(byteBuffer);
        }
    }
}
